var app = getApp(), fetchApi = require("../../../utils/httpPost.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    qrCodeImg:'',
    state:0,
    qrcode_type:'',
    order_info_id:0,
    send: [],
    receive: [],
    infos:[],
    orderInfo:[],
    order_id:0,
    nickName:'',
    isLoading: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.info_id){
      this.data.order_info_id = parseInt(options.info_id);
      this.onHttpOrderDetail(this.data.order_info_id)
      this.initWebsocket();
    }
  },
  onHttpOrderDetail: function (info_id){
    var that = this;
    fetchApi.httpPost('/product/order/info/detail', {
      info_id: info_id,
      reset_qr_code:1,
    }, function (res) {
      console.log("获取详情二维码数据", res)
      if (res.data.code == 0) {
        that.setData({ 
          orderInfo:res.data.data.orderInfo,
          nickName:res.data.data.nickName,
          state:0,
        })
      }else{
        that.setData({ state: 1, error: res.data.msg})
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  onUnload: function () {
  	//返回操作
    console.log('onUnload');
    //注意这里有时序问题，
    //如果 wx.connectSocket 还没回调 wx.onSocketOpen，而先调用 wx.closeSocket，那么就做不到关闭 WebSocket 的目的。
    //必须在 WebSocket 打开期间调用 wx.closeSocket 才能关闭。
    wx.closeSocket()
    wx.onSocketClose(function(res) {
      console.log('WebSocket 已关闭！')
    })
  },
  initWebsocket:function(){
    var that = this;
    console.log('initWebsocket',this);
    console.log('initWebsocket url ',fetchApi.getWebSokectUrl()  + "/" + that.data.order_id);
    
   var receivetemp = this.data.receive;
    //建立连接
    wx.connectSocket({
      // url: "ws://localhost:8888",
      //webSocketUrl = "wss://www.sdyuedou.com/apiws", 
      url: fetchApi.getWebSokectUrl(),
    })

    //连接成功
    wx.onSocketOpen(function () {
      wx.sendSocketMessage({
        data: 'connect|' + that.data.order_id,
      })
      console.log("连接成功");
    })

    //接收数据
    wx.onSocketMessage(function (data) {
      console.log('客户端回复的消息',data);
      if(data.data!='connect'){
        // console.log("客户端回复的消息" , data.data);
        // receivetemp.push(data.data);
        // that.setData({
        //   receive: receivetemp
        // })
        var retdat = JSON.parse(data.data)
        if(retdat.code == 0){
          var order_id = retdat.data.order_id
          wx.redirectTo({
            url: '/pages/pay/payResult/index?order_id=' + order_id,
          })
        }else{
        console.log('失败',data);
        }
      }
    })

    //连接失败
    wx.onSocketError(function () {
      console.log('websocket连接失败！');
    })
  },
  goGetQrCode:function(){
    if (this.data.qrcode_type == 'orderInfo') {
      this.onHttpOrderDetail(this.data.order_id);
    }else{
      this.setData({ state: 2 })
      this.onHttpPersonalQrcodeInit()
    }
  },
  onClickShowQrCode(e){
    var index = e.currentTarget.dataset.index;
    this.data.order_info_id = this.data.infos[index].id;
    this.setData({
      qrCodeImg:this.data.infos[index].qrCode,
      state:0,
    })
  },
  onHttpPersonalQrcodeInit: function () {
    var that = this;
    // that.setData({
    //   qrCodeImg: '',
    // })
    fetchApi.httpPost('/product/order/resetOrderInfoQrCode', {
      info_id:that.data.order_info_id ,
    }, function (res) {
      console.log("获取二维码数据", res)
      if (res.data.code == 0) {
        console.log('qrCode',res.data.data.orderInfo.qrCode)
        that.setData({
          qrCodeImg: res.data.data.orderInfo.qrCode,
        })
      } else {
        that.setData({ state: 1, error: res.data.msg })
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  }
})